package com.huxin.servelt.admin;

import com.alibaba.fastjson.JSON;
import com.huxin.pojo.BookStorage;
import com.huxin.pojo.ConditionalQueryDTO;
import com.huxin.pojo.PageBean;
import com.huxin.service.Userservice;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/selectallBookStorage3")
public class SelectAllBooksStorage3Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求方法
        request.setCharacterEncoding("utf-8");
        //处理响应乱码
        response.setContentType("text/json;charset=utf-8");
        String s = request.getReader().readLine();
        ConditionalQueryDTO conditionalQueryDTO = JSON.parseObject(s, ConditionalQueryDTO.class);
        conditionalQueryDTO.setPage(1);
        conditionalQueryDTO.setSize(10);
        PageBean<BookStorage> bean=null;
        Userservice userservice = new Userservice();


        if (conditionalQueryDTO==null||conditionalQueryDTO.getText().equals("")||conditionalQueryDTO.getText().equals("null")){//如果文本框没输入内容 就查询所有
//            booksInfoArrayList=userservice.selectAllBooks();
            bean=userservice.findAllBookStorage(1,10);
        } else if ("author".equals(conditionalQueryDTO.getType())) {//按照作者查询
            bean=userservice.booksConditionalQueryByAuthor1(conditionalQueryDTO,conditionalQueryDTO.getPage(),conditionalQueryDTO.getSize());
        } else if ("type".equals(conditionalQueryDTO.getType()  )) {//分类
            bean=userservice.booksSConditionalQueryByType(conditionalQueryDTO,conditionalQueryDTO.getPage(),conditionalQueryDTO.getSize());
        } else if ("bid".equals(conditionalQueryDTO.getType())) {//书籍号
            bean=userservice.booksSConditionalQueryByBid(conditionalQueryDTO,conditionalQueryDTO.getPage(),conditionalQueryDTO.getSize());
        }else if ("name".equals(conditionalQueryDTO.getType())){//小说名字查询
            bean=userservice.booksSConditionalQueryByName(conditionalQueryDTO,conditionalQueryDTO.getPage(),conditionalQueryDTO.getSize());
        } else if ("all".equals(conditionalQueryDTO.getType())) {
//            booksInfoArrayList=userservice.selectAllBooks();
            bean=userservice.findAllBooksS(conditionalQueryDTO.getPage(),conditionalQueryDTO.getSize());
        } else if (null==conditionalQueryDTO.getText()||"null".equals(conditionalQueryDTO.getText())){
//            booksInfoArrayList=userservice.selectAllBooks();
            bean=userservice.findAllBookStorage(conditionalQueryDTO.getPage(),conditionalQueryDTO.getSize());
        }


        String jsonString = JSON.toJSONString(bean);
        response.getWriter().write(jsonString);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
